
package com.the9tcat.funnyalarmclock.oauth;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * The Class Oauth.
 * 
 * @author jianhpin
 */
public class Oauth {

    Context mContext;

    String consumer_key;

    String consumer_secret;

    public Oauth(Context con, String consumer_key, String consumer_secret) {
        mContext = con;
        this.consumer_key = consumer_key;
        this.consumer_secret = consumer_secret;
    }

    /** 从Oauth服务商处得到信息，请先得到授权 */
    public static String getAuthorizedContent(String requestUrl,
            String consumer_key, String consumer_secret, String oauth_token,
            String oauth_token_secret, List<OauthParams> provider_defined) {
        String oauth_nonce = getNonce();
        String oauth_timestamp = System.currentTimeMillis() + "";

        OauthParams op1 = new OauthParams("oauth_consumer_key", consumer_key);
        OauthParams op2 = new OauthParams("oauth_nonce", oauth_nonce);
        OauthParams op3 = new OauthParams("oauth_signature_method",
                                          OauthConfig.oauth_signature_method);
        OauthParams op4 = new OauthParams("oauth_timestamp", oauth_timestamp);
        OauthParams op5 = new OauthParams("oauth_token", oauth_token);
        OauthParams op6 = new OauthParams("oauth_version", "1.0");
        List<OauthParams> params = new ArrayList<OauthParams>();
        params.add(op1);
        params.add(op2);
        params.add(op3);
        params.add(op4);
        params.add(op5);
        params.add(op6);
        if (provider_defined != null && provider_defined.size() != 0) {
            params.addAll(provider_defined);
        }
        OauthRequest or = new OauthRequest(requestUrl,
                                           OauthConfig.http_method_post, params);
        String result = or.resultByPOST(consumer_secret + "&"
                + oauth_token_secret);
        return result.replaceAll("\n", "");
    }

    /**
     * 第三步 获取授权的Access Token
     * 
     * @param request_url the request_url
     */
    public String requestAccessToken(String request_url, String oauth_token,
            String oauth_token_secret, String oauth_verifier) {
        String oauth_nonce = getNonce();
        String oauth_timestamp = System.currentTimeMillis() + "";
        OauthParams op1 = new OauthParams("oauth_consumer_key", consumer_key);
        OauthParams op2 = new OauthParams("oauth_nonce", oauth_nonce);
        OauthParams op3 = new OauthParams("oauth_signature_method",
                                          OauthConfig.oauth_signature_method);
        OauthParams op4 = new OauthParams("oauth_timestamp", oauth_timestamp);
        OauthParams op5 = new OauthParams("oauth_token", oauth_token);
        OauthParams op6 = new OauthParams("oauth_token_secret",
                                          oauth_token_secret);
        OauthParams op7 = new OauthParams("oauth_verifier", oauth_verifier);
        OauthParams op8 = new OauthParams("oauth_version",
                                          OauthConfig.oauth_version);
        List<OauthParams> params = new ArrayList<OauthParams>();
        params.add(op1);
        params.add(op2);
        params.add(op3);
        params.add(op4);
        params.add(op5);
        params.add(op6);
        params.add(op7);
        params.add(op8);
        OauthRequest or = new OauthRequest(request_url,
                                           OauthConfig.http_method_post, params);
        String key = consumer_secret + "&" + oauth_token_secret;
        String result = or.resultByPOST(key).replaceAll("\n", "");
        return result;
    }

    public void userAuthorize(String request_url, OauthListener listener) {
        new OauthDialog(mContext, request_url, listener).show();
    }

    /**
     * 第一步 获取未授权的oauth_token和oauth_token_secret.
     * 
     * @param request_url the request_url
     */
    public String requestOauthToken(String request_url) {
        String oauth_nonce = getNonce();
        String oauth_timestamp = System.currentTimeMillis() + "";

        OauthParams op1 = new OauthParams("oauth_consumer_key", consumer_key);
        OauthParams op2 = new OauthParams("oauth_nonce", oauth_nonce);
        OauthParams op3 = new OauthParams("oauth_signature_method",
                                          OauthConfig.oauth_signature_method);
        OauthParams op4 = new OauthParams("oauth_timestamp", oauth_timestamp);
        OauthParams op5 = new OauthParams("oauth_callback",
                                          OauthConfig.oauth_callback);
        OauthParams op6 = new OauthParams("oauth_version", "1.0");
        List<OauthParams> params = new ArrayList<OauthParams>();
        params.add(op1);
        params.add(op2);
        params.add(op3);
        params.add(op4);
        params.add(op5);
        params.add(op6);
        OauthRequest or = new OauthRequest(request_url,
                                           OauthConfig.http_method_post, params);
        String result = or.resultByPOST(consumer_secret + "&").replaceAll("\n",
                                                                          "");
        return result;
    }

    /**
     * Gets the nonce.
     * 
     * @return the nonce
     */
    private static String getNonce() {
        final String CHAR = "0123456789ABCDEFGHIJKLMNOPQRSTUV"
                + "WXYZabcdefghijklmnopqrstuvwxyz";
        Random ran = new Random(System.currentTimeMillis());
        // Random length of nonce string, from 8 to 25 here
        int len = ran.nextInt(17) + 8;
        StringBuffer sb = new StringBuffer(len);
        for (int i = 0; i < len; i++) {
            int index = ran.nextInt(62);
            sb.append(CHAR.charAt(index));
        }
        return sb.toString();
    }
}
